import finance.User 

class SecurityController {
	
	def index = {
	}
	
	def login_required = {
		render(view: 'login_error')
	}
	
	def login = {
		User user = User.find('from User u where u.user_name = ? and u.password = ?',  [params.user_name, params.password])	
		if(user) {
			session.user = user
			redirect(controller: 'home', action:'list_accounts')
		} else {
			render(view: 'login_error')
		}
	}
	
	def register = {
		User user = User.find('from User u where u.user_name = ?', [params.user_name])
		if(user) {
			render(view: 'register_error')
		} else {
			User new_user = new User(user_name: params.user_name, password: params.password)
			if(new_user.save(flush:true)) {
				render(view: 'register_ok')
			}
			render(view: 'register_error')
		}
	}
	
	def logout = {
		session.user = null
	}
}